import java.util.PriorityQueue;

class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> {
            return b-a;
        });
        for (int i = 0; i < stones.length; i++) {
            maxHeap.offer(stones[i]);
        }
        while (maxHeap.size() >= 2) {
            int x = maxHeap.poll(), y = maxHeap.poll();
            int ans = Math.abs(x - y);
            if (ans != 0) { // 当剩余石头时，才需要入堆
                maxHeap.offer(ans);
            }
        }
        // 如果堆中还有元素，就返回最终元素的值，如果没有元素，就返回0
        return maxHeap.size() == 0 ? 0 : maxHeap.poll();
    }
}